package com.bt.lms.network.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bt.lms.bo.TeachingPlanCondition;
import com.bt.lms.constant.BOConstant;
import com.bt.lms.constant.TeachingPlanBizTypeConstant;
import com.bt.lms.entity.TeachingPlanBasicInfo;
import com.bt.lms.bo.Page;
import com.bt.lms.network.msg.ReqMsg;
import com.bt.lms.network.msg.RespMsg;
import com.bt.lms.service.TeachingPlanService;
import com.bt.lms.service.impl.TeachingPlanServiceImpl;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 控制器
 * @author 李舒淇
 */
public class TeachingPlanModuleHandler implements ModuleHandler {


    @Override
    public void handlerMsg(ReqMsg reqMsg, Socket socket) {
        //1、获取类型
        Short bizType = reqMsg.getBizType();
        //2、处理数据
        String data = reqMsg.getData();
        //3、调用业务接口
        //消息转对象
        Object obj = dizAdd(bizType, data);
        //4、是否响应
        Byte twoWay = reqMsg.getTwoWay();
        if (twoWay.equals(ReqMsg.NETWORK_TWO_WAY_CHAT)) {

            RespMsg respMsg = objReq(reqMsg, obj);
            byte[] msgByte = respMsg.toBytes();
            //响应
            try {
                OutputStream out = socket.getOutputStream();
                out.write(msgByte);
                out.flush();
                out.close();
                socket.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }


        }

    }

    /**
     * 组包
     * @param reqMsg  请求消息
     * @param obj  对象
     * @return  响应消息
     */
    private RespMsg objReq(ReqMsg reqMsg, Object obj) {

        String magicNo = reqMsg.getMagicNo();//魔数
        Byte version = reqMsg.getVersion();//协议版本号
        Byte serialCou = reqMsg.getSerialCou();//序列化算法
        Byte evenType = reqMsg.getEvenType();//事件类型
        Short bizType = reqMsg.getBizType();//业务类型
        Long reqId = reqMsg.getReqId();//请求 id
        byte[] remainField = reqMsg.getRemainField();//保留字段
        String objStr = JSON.toJSONString(obj);//数据

        //响应消息
        RespMsg respMsg = new RespMsg();
        respMsg.setMagicNo(magicNo);//魔数
        respMsg.setVersion(version);//协议版本号
        respMsg.setSerialCou(serialCou);//序列化算法
        respMsg.setEvenType(evenType);//事件类型
        respMsg.setBizType(bizType);//业务类型
        respMsg.setReqId(reqId);//请求id
        respMsg.setRemainField(remainField);//保留字段
        respMsg.setStatus(RespMsg.NETWORK_STATUS_NORMAL);//状态
        respMsg.setData(objStr);//数据

        return respMsg;
    }

    //调用业务接口
    private Object dizAdd(Short bizType, String data) {

        Object obj = null;
        //业务接口
        TeachingPlanService teachingPlanService = new TeachingPlanServiceImpl();

        //新增
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_TEACHING_ADD)) {

            //消息转对象
            TeachingPlanBasicInfo teachingPlanBasicInfo = JSONObject.parseObject(data, TeachingPlanBasicInfo.class);
            //调用业务接口
            try {
                obj = teachingPlanService.add(teachingPlanBasicInfo);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

        //关联课程
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_RELEVANCE_COURSE)){
//
//            //消息转对象
//            TeachingPlanBasicInfo teachingPlanBasicInfo = JSONObject.parseObject(data, TeachingPlanBasicInfo.class);
//            //调用业务接口
//            try {
//                obj = teachingPlanService.relevancyCourse();
//            } catch (Exception e) {
//                throw new RuntimeException(e);
//            }
        }

        //查看详情
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_DETAILS)){
        }

        //确认完成
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_ACCOMPLISH)){

        }

        //启用
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_ENABLE)){

        }

        //编辑
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_EDIT)){

        }

        //删除
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_DELETE)){

        }

        //分页条件查询
        if (bizType.equals(TeachingPlanBizTypeConstant.PLAN_QUERY)){

            Page<TeachingPlanCondition> objectPage = new Page<>();

            Page teachingPage = JSONObject.parseObject(data, Page.class);
            obj = teachingPlanService.query(objectPage, teachingPage);


        }


        return obj;
    }







}
